package com.zzyl.nursing.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zzyl.common.core.controller.BaseController;
import com.zzyl.common.core.domain.AjaxResult;
import com.zzyl.common.core.domain.R;
import com.zzyl.common.core.page.TableDataInfo;
import com.zzyl.nursing.dto.NursingTaskDto;
import com.zzyl.nursing.dto.NursingTaskPageDto;
import com.zzyl.nursing.service.INursingTaskService;
import com.zzyl.nursing.vo.NursingTaskPageVo;
import com.zzyl.nursing.vo.QueryNursingTaskDetailVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 护理任务Controller
 * 
 * @author zzyl
 * @author nmy
 * @date 2025-10-29
 */
@Slf4j
@RestController
@RequestMapping("/nursing/nursingTask")
@Api(tags = "护理任务相关接口")
public class NursingTaskController extends BaseController {
    @Autowired
    private INursingTaskService nursingTaskService;

    @ApiOperation("取消任务")
    @PutMapping("/cancel")
    @PreAuthorize("@ss.hasPermi('nursing:nursingTask:edit')")
    public AjaxResult cancel(@RequestBody NursingTaskDto dto) {
        log.info("取消任务--取消原因为：{}，护理任务ID为：{}", dto.getReason(), dto.getTaskId());
        nursingTaskService.cancel(dto.getReason(), dto.getTaskId());
        return AjaxResult.success();
    }

    /**
     * 查询护理任务列表
     */
    @PreAuthorize("@ss.hasPermi('nursing:nursingTask:list')")
    @GetMapping("/list")
    @ApiOperation(value = "获取护理任务列表")
    public TableDataInfo<List<NursingTaskPageVo>> list(NursingTaskPageDto dto) {
        Page<NursingTaskPageVo> page = nursingTaskService.selectNursingTaskList(dto);
        TableDataInfo dataTable = getDataTable(page.getRecords());
        dataTable.setTotal(page.getTotal());
        return dataTable;
    }

    @ApiOperation("任务执行")
    @PutMapping("/do")
    @PreAuthorize("@ss.hasPermi('nursing:nursingTask:edit')")
    public AjaxResult doNursingTask(@RequestBody NursingTaskDto dto) {
        log.info("任务执行--执行时间为：{}，执行记录为：{}，护理任务ID为：{}，任务图片为：{}", dto.getEstimatedServerTime(), dto.getMark(), dto.getTaskId(), dto.getTaskImage());
        nursingTaskService.doNursingTask(dto.getEstimatedServerTime(), dto.getMark(), dto.getTaskId(), dto.getTaskImage());
        return AjaxResult.success();
    }

    /**
     * 获取护理任务详细信息
     */
    @PreAuthorize("@ss.hasPermi('nursing:nursingTask:query')")
    @GetMapping(value = "/{id}")
    @ApiOperation("获取护理任务详细信息")
    public R<QueryNursingTaskDetailVo> getInfo(@ApiParam(value = "护理任务ID", required = true)
                                               @PathVariable("id") Long id) {
        return R.ok(nursingTaskService.selectNursingTaskById(id));
    }


    @PutMapping("/updateTime")
    @ApiOperation("任务改期")
    @PreAuthorize("@ss.hasPermi('nursing:nursingTask:edit')")
    public AjaxResult updateTime(@RequestBody NursingTaskDto dto) {
        log.info("任务改期--更改预计执行时间为：{}，护理任务ID为：{}", dto.getEstimatedServerTime(), dto.getTaskId());
        nursingTaskService.updateTime(dto.getEstimatedServerTime(), dto.getTaskId());
        return AjaxResult.success();
    }
}
